﻿using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class ServerCmdParser 
{
    //未划分模块的消息解析

    //登录 C-->S

    //解析用户账号和密码，向客户端发送角色列表
    public static void OnLogin(Cmd cmd)
    {
        if (!Client.CherkCmd(cmd, typeof(LoginCmd)))
            return;
        //Debug.Log("分发LoginCmd成功");
        LoginCmd loginCmd = cmd as LoginCmd;

        //验证账号密码

        //找到玩家的存档
        var playerData = Server.GetInstance().DB.GetUserData(1);
        //如果没有玩家的数据存档，就默认生成一个假的数据存档
        if (playerData == null)
        {
            playerData = new Player();
            playerData.ThisID = 0;
            Server.GetInstance().DB.SavePlayerData(playerData);
        }
        Server.GetInstance().CurPlayer = playerData;

        var player = Server.GetInstance().CurPlayer;

        //向客户端发送玩家的已创建的角色列表
        RoleListCmd roleListCmd = new RoleListCmd();
        foreach (var role in player.AllRole)
        {
            var roleInfo = new RoleServer() { Name = role.Name, ModelID=role.ModelID};
            roleListCmd.AllRole.Add(roleInfo);
        }
        Server.GetInstance().SendCmd(roleListCmd);
    }


    //服务器解析玩家
    public static void OnCreateScene(Cmd cmd)
    {
        if (!Client.CherkCmd(cmd, typeof(RoleIndexCmd)))
            return;
        RoleIndexCmd onRoleSelect = cmd as RoleIndexCmd;

        var curPlayer = Server.GetInstance().CurPlayer;
        RoleServer curRole = curPlayer.AllRole[onRoleSelect.Index];
        
        //读取Role表格中的数据，复制给服务器
        curPlayer.CurRole = curRole;
        curPlayer.CurRole.Attack = RoleTable.GetInstance()[onRoleSelect.Index].Attack;
        curPlayer.CurRole.HP = RoleTable.GetInstance()[onRoleSelect.Index].HP;
        curPlayer.CurRole.Defance = RoleTable.GetInstance()[onRoleSelect.Index].Defence;

        //分配ThisID
        var thisID = RoleServer.GetNewThisID();
        curPlayer.CurRole.ThisID = thisID;

        //告诉客户端主角的ThisID
        MainRoleThisIdCmd mainRoleThisIdCmd = new MainRoleThisIdCmd();
        mainRoleThisIdCmd.thisID = thisID;
        Server.GetInstance().SendCmd(mainRoleThisIdCmd);

        //进入场景
        enterMap(curRole,1);
    }

    private static void enterMap(RoleServer curRole,int sceneID)
    {
        //告诉客户端,场景编号
        var _sceneID = sceneID;
        EnterMapCmd enterMapCmd = new EnterMapCmd() { MapID = _sceneID };
        //生成主角
        CreateSenceRoleCmd roleCmd = new CreateSenceRoleCmd();
        roleCmd.ThisID = curRole.ThisID;
        roleCmd.Name = curRole.Name;
        roleCmd.ModelID = curRole.ModelID;
        roleCmd.Pos = Vector3.zero;
        roleCmd.FaceTo = Vector3.up;
        roleCmd.Attack = curRole.Attack;
        roleCmd.HP = curRole.HP;
        roleCmd.MaxHP = curRole.HP;
        roleCmd.Defence = curRole.Defance;

        //向客户端发送消息
        Server.GetInstance().SendCmd(enterMapCmd);
        Server.GetInstance().SendCmd(roleCmd);
        //生成附近的配角
        //生成附近的NPN
        CreatSomeNpc();
    }

    private static void CreatSomeNpc()
    {
        var npc1Cmd = new CreateSenceNpcCmd();
        npc1Cmd.ThisID = RoleServer.GetNewThisID();
        npc1Cmd.ModelID = NpcTable.GetInstance()[1].ID;
        var npc1Database = NpcTable.GetInstance()[npc1Cmd.ModelID];
        npc1Cmd.Name = npc1Database.Name;
        npc1Cmd.Attack = npc1Database.Attack;
        npc1Cmd.Defence = npc1Database.Defence;
        npc1Cmd.HP = npc1Database.HP;
        npc1Cmd.MaxHP = npc1Database.HP;
        npc1Cmd.Pos = new Vector3(0, 0, 1);
        npc1Cmd.FaceTo = Vector3.forward;

        var npc2Cmd = new CreateSenceNpcCmd();
        npc2Cmd.ThisID = RoleServer.GetNewThisID();
        npc2Cmd.ModelID = NpcTable.GetInstance()[2].ID;
        var npc2Database = NpcTable.GetInstance()[npc2Cmd.ModelID];
        npc2Cmd.Name = npc2Database.Name;
        npc2Cmd.Attack = npc2Database.Attack;
        npc2Cmd.Defence = npc2Database.Defence;
        npc2Cmd.HP = npc2Database.HP;
        npc2Cmd.MaxHP = npc2Database.HP;
        npc2Cmd.Pos = new Vector3(0, 0, 2);
        npc2Cmd.FaceTo = Vector3.forward;

        var npc3Cmd = new CreateSenceNpcCmd();
        npc3Cmd.ThisID = RoleServer.GetNewThisID();
        npc3Cmd.ModelID = NpcTable.GetInstance()[3].ID;
        var npc3Database = NpcTable.GetInstance()[npc3Cmd.ModelID];
        npc3Cmd.Name = npc3Database.Name;
        npc3Cmd.Attack = npc3Database.Attack;
        npc3Cmd.Defence = npc3Database.Defence;
        npc3Cmd.HP = npc3Database.HP;
        npc3Cmd.MaxHP = npc3Database.HP;
        npc3Cmd.Pos = new Vector3(0, 0, 3);
        npc3Cmd.FaceTo = Vector3.forward;

        Server.GetInstance().SendCmd(npc1Cmd);
        Server.GetInstance().SendCmd(npc2Cmd);
        Server.GetInstance().SendCmd(npc3Cmd);

    }

    //服务器解析跳转地图信息
    public static void OnJumpTo(Cmd cmd)
    {
        if (!Client.CherkCmd(cmd, typeof(JumpToCmd)))
            return;
        JumpToCmd jumpToCmd = cmd as JumpToCmd;
        enterMap(Server.GetInstance().CurPlayer.CurRole, 1);
    }
}
